package com.ecoolex.hotel.saas.platform.graphql;

import java.util.List;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.annotation.PlatformAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.auth.platform.PlatformMemberAuthority;
import com.ecoolex.framework.modularize.api.service.storage.FileSize;
import com.ecoolex.framework.modularize.api.service.storage.MimeType;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.modularize.api.service.storage.Upload;
import com.ecoolex.framework.modularize.api.service.storage.UploadLimit;
import com.ecoolex.framework.modularize.api.service.storage.UploadSource;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.hotel.saas.entity.PresaleCoupon;
import com.ecoolex.hotel.saas.entity.PresaleCouponCategory;
import com.ecoolex.hotel.saas.entity.PresaleCouponCategoryJoint;
import com.ecoolex.hotel.saas.platform.core.PlatformConstant;
import com.ecoolex.hotel.saas.pojo.AddPresaleCouponCategoryDTO;
import com.ecoolex.hotel.saas.pojo.AddPresaleCouponCategoryJointDTO;
import com.ecoolex.hotel.saas.pojo.EditPresaleCouponCategoryDTO;
import com.ecoolex.hotel.saas.pojo.EditPresaleCouponCategoryJointDTO;
import com.ecoolex.hotel.saas.ws.OperationService;
import com.ecoolex.hotel.saas.ws.PresaleCouponService;

@GraphQL2Module(type = Type.Platform, description = "预售券分类")
@PlatformAuthoritativeRegister(parentKey = PlatformConstant.AUTH_PROJECT_KEY,
	key = PresaleCouponCategoryGraphql.AUTH,
	name = "预售券分类管理",
	remark = "预售券分类管理")
public class PresaleCouponCategoryGraphql {

	public static final String AUTH = PlatformConstant.AUTH_PROJECT_KEY + "-PRESALE-COUPON-CATEGORY";
	
	private static final String AUTH_ADD = AUTH + "-ADD";
	
	private static final String AUTH_EDIT = AUTH + "-EDIT";
	
	private static final String AUTH_GET = AUTH + "-GET";
	
	private static final String AUTH_QUERY = AUTH + "-QUERY";
	
	private static final String AUTH_DEL = AUTH + "-DEL";
	
	private static final String AUTH_GET_JOINT = AUTH + "-JOINT-GET";
	
	private static final String AUTH_QUERY_JOINT = AUTH + "-JOINT-QUERY";
	
	private static final String AUTH_ADD_JOINT = AUTH + "-JOINT-ADD";
	
	private static final String AUTH_EDIT_JOINT = AUTH + "-JOINT-EDIT";
	
	private static final String AUTH_DEL_JOINT = AUTH + "-JOINT-DEL";
	
	@Autowired
	private StorageService storageService;
	
	@Reference
	private OperationService operationService;
	
	@Reference
	private PresaleCouponService presaleCouponService;
	
	@TypeField(type = PresaleCoupon.class, field = "isRelationCategory", description = { "是否关联了分类" })
	public Boolean isRelationCategory(@Source PresaleCoupon source, @NonNull Integer categoryId) {
		return operationService.isRelationCategory(source.getPreCouponId(), categoryId);
	}
	
	@TypeField(type = PresaleCouponCategoryJoint.class, field = "presaleCoupon", description = { "预售券" })
	public PresaleCoupon presaleCoupon(@Source PresaleCouponCategoryJoint source) {
		return presaleCouponService.getPresaleCoupon(source.getPreCouponId());
	}
	
	@TypeField(type = PresaleCouponCategory.class, field = "presaleCouponCount", description = { "关联的预售券数量" })
	public Long count(@Source PresaleCouponCategory source) {
		return operationService.presaleCouponJointCount(source.getCategoryId());
	}
	
	@TypeField(type = PresaleCouponCategory.class, field = "image", description = { "图片" })
	public StorageInfo image(@Source PresaleCouponCategory source) {
		return storageService.get(source.getImage());
	}
	
	
	@QueryField(field =  "presaleCouponCategory", description = "预售券分类详情")
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "预售券分类详情")
	public PresaleCouponCategory get(@NonNull Integer categoryId, PlatformMemberAuthority user) {
		user.assertAuth(AUTH_GET);
		return operationService.getPresaleCouponCategory(categoryId);
	}
	
	@QueryField(field =  "presaleCouponCategoryList", description = "预售券分类列表")
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "预售券分类列表")
	public Pagination<PresaleCouponCategory> query(String name,@NonNull Integer storeId, @NonNull Page page, PlatformMemberAuthority user){
		user.assertAuth(AUTH_QUERY);
		return operationService.queryPresaleCouponCategory(storeId, name, page);
	}
	
	@MutationField(field = "addPresaleCouponCategory", description = "添加预售券分类")
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加预售券分类")
	public PresaleCouponCategory add(@NonNull AddPresaleCouponCategoryDTO add, PlatformMemberAuthority user) {
		user.assertAuth(AUTH_ADD);
		return operationService.addPresaleCouponCategory(add, user.userName());
	}
	
	@MutationField(field = "editPresaleCouponCategory", description = "编辑预售券分类")
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑预售券分类")
	public PresaleCouponCategory edit(@NonNull EditPresaleCouponCategoryDTO edit, PlatformMemberAuthority user) {
		user.assertAuth(AUTH_EDIT);
		PresaleCouponCategory old = operationService.getPresaleCouponCategory(edit.getCategoryId());
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "预售券分类不存在");
		BeanUtil.copy(edit, old);
		return operationService.editPresaleCouponCategory(old, user.userName());
	}
	
	@MutationField(field = "delPresaleCouponCategory", description = "删除预售券分类")
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除预售券分类")
	public Boolean del(@NonNull Integer categoryId,  PlatformMemberAuthority user) {
		user.assertAuth(AUTH_DEL);
		PresaleCouponCategory old = operationService.getPresaleCouponCategory(categoryId);
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "预售券分类不存在");
		return operationService.delPresaleCouponCategory(old, user.userName());
	}
	
	@MutationField(field = "uploadPresaleCouponCategoryImage", description = { "上传预售券分类图片" })
	public StorageInfo uploadHeadImage(@NonNull Upload request, PlatformMemberAuthority user) {
		if (!user.auth(AUTH_EDIT)) {
            user.notAuthority();
        }
        UploadSource uploadSource = new UploadSource("PresaleCouponCategory", "Image");
        UploadLimit uploadLimit = new UploadLimit();
        uploadLimit.setMaxSize(FileSize.MB.byteLength(10));
        uploadLimit.setAllowMimeType(MimeType.IMAGE.commons());

        return storageService.upload(request, uploadSource, uploadLimit);
	}
	
	@QueryField(field =  "presaleCouponCategoryJoint", description = "分类关联预售券详情")
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET_JOINT, name = "分类关联预售券详情")
	public PresaleCouponCategoryJoint getPresaleCouponCategoryJoint(Integer id, PlatformMemberAuthority user) {
		user.assertAuth(AUTH_GET_JOINT);
		return operationService.getPresaleCouponCategoryJoint(id);
	}
	
	@QueryField(field =  "presaleCouponCategoryJointList", description = "分类关联预售券列表")
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY_JOINT, name = "分类关联预售券列表")
	public Pagination<PresaleCouponCategoryJoint> queryPresaleCouponCategoryJoint(@NonNull Integer categoryId,
		String presaleCouponName,@NonNull Page page, PlatformMemberAuthority user) {
		user.assertAuth(AUTH_QUERY_JOINT);
		return operationService.queryPresaleCouponCategoryJoint(categoryId, presaleCouponName, page);
	}
	
	@MutationField(field = "addPresaleCouponCategoryJoint", description = "添加分类关联的预售券")
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD_JOINT, name = "添加分类关联的预售券")
	public Boolean addPresaleCouponCategoryJoint(@NonNull List<AddPresaleCouponCategoryJointDTO> addList, PlatformMemberAuthority user) {
		user.assertAuth(AUTH_ADD_JOINT);
		return operationService.addPresaleCouponCategoryJoint(addList, user.userName());
	}
	
	@MutationField(field = "editPresaleCouponCategoryJoint", description = "编辑分类关联的预售券权重")
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT_JOINT, name = "编辑分类关联的预售券权重")
	public PresaleCouponCategoryJoint editPresaleCouponCategoryJoint(@NonNull EditPresaleCouponCategoryJointDTO edit, PlatformMemberAuthority user) {
		user.assertAuth(AUTH_EDIT_JOINT);
		PresaleCouponCategoryJoint old = operationService.getPresaleCouponCategoryJoint(edit.getId());
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "分类关联预售券信息不存在");
		old.setWeight(edit.getWeight());
		return operationService.editPresaleCouponCategoryJoint(old, user.userName());
	}
	
	@MutationField(field = "delPresaleCouponCategoryJoint", description = "删除分类关联的预售券")
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL_JOINT, name = "删除分类关联的预售券")
	public Boolean delPresaleCouponCategoryJoint(@NonNull Integer id, PlatformMemberAuthority user) {
		user.assertAuth(AUTH_DEL_JOINT);
		PresaleCouponCategoryJoint old = operationService.getPresaleCouponCategoryJoint(id);
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "分类关联预售券信息不存在");
		return operationService.delPresaleCouponCategoryJoint(old, user.userName());
	}
}
